package com;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.milling.common.utils.DateUtils;
import com.milling.common.utils.http.HttpUtils;
import com.milling.common.utils.uuid.UUID;

import java.awt.event.ItemEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class TestPostJsonobject {


    public static void mainCC(String[] args) {
        String temp = "{\"370000000001\":[238],\"275000000001\":[232],\"110000000001\":[220],\"130070001101\":[197,198,199,200,597,598,599,600],\"100045001101\":[184,185,186,187,188,189,190,191,192,193,194,195,196,217,584,585,586,587,588,589,590,591,592,593,594,595,596],\"105000000001\":[219],\"152500000001\":[226],\"030045000301\":[203,204],\"450000000001\":[240],\"320000000001\":[237],\"080045001101\":[213],\"025040000401\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,175,176,177,178,179,202,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,575,576,577,578,579],\"120065001101\":[222],\"050070000301\":[206,207],\"020035000401\":[50,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,201,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574],\"075070000301\":[211],\"630000000001\":[242],\"475000000001\":[241],\"060045001101\":[208],\"185000000001\":[227],\"317500000001\":[234,235,236],\"135000000001\":[224],\"150000000001\":[225],\"040055000301\":[205],\"101600000001\":[218],\"100000000001\":[215,216],\"400000000001\":[239],\"250000000001\":[231],\"132100000001\":[223],\"115000000001\":[221],\"090095000301\":[214],\"243800000001\":[230],\"225000000001\":[229],\"060070000301\":[180,181,182,183,580,581,582,583],\"300000000001\":[233],\"080070000301\":[212],\"205000000001\":[228],\"070045001101\":[209,210]}\n";
        JSONObject jsonObject = JSONObject.parseObject(temp);
        Map<String, List<String>> map = new HashMap<>();

        for (String key : jsonObject.keySet()) {
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            List<String> stringList = jsonArray.stream()
                    .map(Object::toString)
                    .collect(Collectors.toList());
            map.put(key, stringList);
        }

        // 打印结果以验证
        Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<String>> entry = iterator.next();
            System.out.println("Key: " + entry.getKey() + ", Values: " + entry.getValue());
        }


    }


    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("MacCode", "D001");
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());

    /*    String jsonString = jsonObject.toJSONString();
        System.out.println("请求包：" + jsonString);
        String response = HttpUtils.postData("http://192.168.237.200:5001/RestAPI/QueryInventory", jsonString);


        JSONObject jsonObject2 = JSONObject.parseObject(response);
        System.out.println("返回包：" + response);
        // 解析JSON字符串
//        JSONObject jsonObject = JSONObject.parseObject(jsonString);

        // 提取顶层字段
        String code = jsonObject2.getString("Code");
        String message = jsonObject2.getString("Message");
        String path = jsonObject2.getString("Path");
        String ip = jsonObject2.getString("Ip");
        String timestamp = jsonObject2.getString("Timestamp");
        String timespan = jsonObject2.getString("Timespan");

        System.out.println("Code: " + code);
        System.out.println("Message: " + message);
        System.out.println("Path: " + path);
        System.out.println("Ip: " + ip);
        System.out.println("Timestamp: " + timestamp);
        System.out.println("Timespan: " + timespan);

        // 提取DataList数组
        JSONArray dataList = jsonObject2.getJSONArray("DataList");
        if (dataList != null) {
            int total = 0;
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject item = dataList.getJSONObject(i);
                String drillInfo = item.getString("drill_info");
                String drillCount = item.getString("drill_count");
                String boxCount = item.getString("box_Count");

                if(new Integer(drillCount).intValue() % 50 == 0){
                    System.out.println("drillInfo: " + drillInfo + " drillCount: " + drillCount + " boxCount: " + boxCount);
                } else {

                }

                total = total + new Integer(drillCount).intValue();
            }
            System.out.println("总量: " + total);

            System.out.println("总料号: " + dataList.size());
        }*/

        //询问二维码的钻针信息
        jsonObject.put("box_id", "920000028895");
        String jsonString = jsonObject.toJSONString();
        System.out.println("请求包：" + jsonString);
        String boxcodeRespones = HttpUtils.postData("http://192.168.237.200:5001/RestAPI/QueryDrillinfoByBoxid", jsonString);
        System.out.println("返回包：" + boxcodeRespones);

        //size_0_3 + type_4 + edge_5 + factory_6 + grind_count_7 + status_8 + "500";
        //临时叫料

        /*String drillSize = "0250";
        String drillType = "5";
        int drillGrindCount = 1;
        String drillCount = "50";
        int boxCount = 1;

        String drillInfo = drillSize + drillType + "11" + drillGrindCount + "D500";
        System.out.println(drillInfo);

        jsonObject.put("m_Drill_Info", drillInfo);
        jsonObject.put("m_Drill_Size", drillSize);
        jsonObject.put("m_Drill_Type", drillType);
        jsonObject.put("m_Drill_Grind_Count", drillGrindCount);

        jsonObject.put("m_Drill_Count", drillCount);//            ‘钻针数量
        jsonObject.put("m_Drill_Box_Count", boxCount);

        jsonObject.put("m_Drill_Edge", "1");
        jsonObject.put("m_Drill_Factory", "1");
        jsonObject.put("m_Drill_Grind_Status", "1");
        jsonObject.put("m_Drill_Asc", "1");
        jsonObject.put("m_Drill_Box_Type", "01");

//        jsonObject.put("deliver_port", 2);//回流输送带
        jsonObject.put("deliver_port", 1);//配针机输送带

        String jsonString = jsonObject.toJSONString();
        System.out.println(jsonString);
        String requestMaterial = HttpUtils.postData("http://192.168.237.200:5001/RestAPI/RequestMaterial", jsonString);
        System.out.println("返回包：" + requestMaterial);*/
    }
}
